package com.finance.config.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 邓天天
 * @date 2021/4/27 20:53
 */
@Configuration
@MapperScan("com.finance.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件配置
     * @return 返回bean
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        var mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 分页插件
        var paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 数据库类型为Mysql
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(500L);

        // 阻止恶意的全表更新和删除 。其实没啥用
        var blockAttackInnerInterceptor =  new BlockAttackInnerInterceptor();

        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        mybatisPlusInterceptor.addInnerInterceptor(blockAttackInnerInterceptor);
        return mybatisPlusInterceptor;
    }
}
